package cn.com.java.service.impl;

import cn.com.java.mapper.SysMenuMapper;
import cn.com.java.model.SysMenu;
import cn.com.java.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Override
    public List<SysMenu> listByParents() {
        return this.baseMapper.listByParents(Boolean.TRUE);
    }

    @Override
    public List<SysMenu> findMenusTree(List<SysMenu> list) {
        List<SysMenu> records = list.stream().map(item -> {
            QueryWrapper<SysMenu> sysMenuQueryWrapper1 = new QueryWrapper<>();
            sysMenuQueryWrapper1.eq("parent_id", item.getId());
            List<SysMenu> childrens = list(sysMenuQueryWrapper1);
            item.setChildren(childrens);
            return item;
        }).collect(Collectors.toList());
        return records;
    }

    @Override
    public List<SysMenu> findSysMensTree() {
        List<SysMenu> list = this.baseMapper.listByParents(Boolean.FALSE);
        return findMenusTree(list);
    }

    @Override
    public List<SysMenu> findMensBySysRoleId(Long sysRoleId) {
        List<SysMenu> mensBySysRoleId = this.baseMapper.findMensBySysRoleId(sysRoleId);
        return findMenusTree(mensBySysRoleId);
    }
}